<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="login-title">设备管理系统登录</div>
      </template>
      
      <!-- 移除不必要的表单验证，由handleLogin函数手动验证 -->
      <el-form :model="loginForm" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            autocomplete="username"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            autocomplete="current-password"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleLogin" 
            :loading="loading"
            style="width: 100%"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <el-alert 
        v-if="errorMessage" 
        :title="errorMessage" 
        type="error" 
        show-icon 
        :closable="false"
        class="error-alert"
      />
    </el-card>
  </div>
</template>

<script>
import { reactive, ref, inject } from 'vue'
import { ElMessage } from 'element-plus'
import router from '../router'

export default {
  name: 'Login',
  setup() {
    const userStore = inject('userStore')
    // 使用reactive替代ref以提升表单输入性能
    const loginForm = reactive({
      username: '',
      password: ''
    })
    const loading = ref(false)
    const errorMessage = ref('')
    
    // 移除表单验证规则，使用简单的手动验证以提升性能
    
    const handleLogin = async () => {
      // 表单验证 - 直接访问reactive对象的属性
      if (!loginForm.username || !loginForm.password) {
        errorMessage.value = '请输入用户名和密码'
        return
      }
      
      loading.value = true
      errorMessage.value = ''
      
      try {
          // 直接传递loginForm对象，不需要.value
          const result = await userStore.login(loginForm)
          if (result.success) {
            ElMessage.success('登录成功')
            // 确保userStore状态更新后再执行跳转，避免App.vue中的watch逻辑干扰
            setTimeout(() => {
              router.push('/')
            }, 100)
          } else {
            errorMessage.value = result.message || '登录失败'
          }
      } catch (error) {
        console.error('登录错误:', error)
        errorMessage.value = '登录失败，请稍后重试'
      } finally {
        loading.value = false
      }
    }
    
    return {
      loginForm,
      loading,
      errorMessage,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 16px;
  box-sizing: border-box;
}

.login-card {
  width: 100%;
  max-width: 400px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.login-title {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
}

.error-alert {
  margin-top: 16px;
}

/* 响应式样式 */
@media (max-width: 768px) {
  .login-container {
    padding: 12px;
  }
  
  .login-card {
    width: 100%;
    max-width: 350px;
  }
  
  .login-title {
    font-size: 18px;
  }
  
  /* 调整表单元素尺寸 */
  :deep(.el-input__inner) {
    padding: 0 12px;
    height: 38px;
  }
  
  :deep(.el-form-item__label) {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    width: 100%;
    max-width: 100%;
    box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.1);
  }
  
  .login-title {
    font-size: 16px;
  }
  
  /* 进一步调整表单元素在小屏幕上的显示 */
  :deep(.el-input__inner) {
    padding: 0 10px;
    height: 36px;
    font-size: 14px;
  }
  
  :deep(.el-button) {
    padding: 10px;
    font-size: 14px;
  }
  
  :deep(.el-form-item) {
    margin-bottom: 16px;
  }
  
  .error-alert {
    margin-top: 12px;
  }
}
</style>